<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>
        添加小区
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/css/main.css}" media="all">
    <script type="text/javascript" th:src="@{/lib/loading/okLoading.js}"></script>
</head>
<style>
    .imgs {
        display: none;
    }
</style>
<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>小区管理</cite></a>
              <a><cite>添加小区</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon"
                                                                        style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <form class="layui-form layui-form-pane" method="post" id="add"
          enctype="multipart/form-data">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <input type="hidden" name="id" value="${param.id}">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>小区编号
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="communityNo" autocomplete="off" placeholder="小区编号"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>小区名称
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="communityName" autocomplete="off" placeholder="小区名称"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>坐落地址
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="address" autocomplete="off" placeholder="坐落地址"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>占地面积
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="area" autocomplete="off" placeholder="占地面积"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>总栋数
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="dong" autocomplete="off" placeholder="总栋数"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>总户数
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="familyNum" autocomplete="off" placeholder="总户数"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>绿化率
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="afforest" autocomplete="off" placeholder="绿化率"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red">*</span>小区缩略图
                        </label>
                        <div class="layui-input-inline">
                            <div class="site-demo-upbar">
                                <div class=" layui-upload-button" style="border:#FFFFFF ;">
                                    <button type="button" class="layui-btn" id="test1">
                                        <i class="layui-icon">&#xe67c;</i>上传图片
                                    </button>
                                    <input class="layui-upload" type="file" accept="undefined" id="previewImg"
                                           name="previewImg" onchange="upload(this)">
                                </div>
                                <!--${message}-->
                            </div>
                        </div>
                        <!--<a href="javascript:;" style="" class="layui-btn " id="cancel"><i class="layui-icon">ဂ</i>撤销上传</a>-->
                    </div>
                    <div class="layui-form-item imgs" id="imgshow">
                        <label class="layui-form-label">缩略图展示</label>
                        <img src="" id="pimages" name="pimages"
                             style="width: 400px;height: 200px;"/>
                        <input id="avatar" name="filePath" type="hidden" value="">
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>开发商名称
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="developers" autocomplete="off" placeholder="开发商名称"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>物业公司名称
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="property" autocomplete="off" placeholder="物业公司名称"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>状态
                        </label>
                        <div class="layui-input-block">
                            <input type="radio" name="state" value="1" title="启用">
                            <div class="layui-unselect layui-form-radio layui-form-radioed"><i
                                    class="layui-anim layui-icon layui-anim-scaleSpring"></i>
                                <div>启用</div>
                            </div>
                            <input type="radio" name="state" value="2" title="停用" checked>
                            <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                                <div>停用</div>
                            </div>
                        </div>
                    </div>
                    <!--<div class="layui-form-item">
                        <label for="link" class="layui-form-label">
                            <span class="x-red">*</span>小区简介
                        </label>
                        <div class="layui-input-inline">
                            <textarea id="content" placeholder="" name="desc"></textarea>
                        </div>
                    </div>-->
                    <div class="layui-form-item" align="center">
                        <button class="layui-btn" type="button" lay-filter="add" lay-submit="">
                            保存
                        </button>
                        <button class="layui-btn" type="reset">
                            重置
                        </button>
                    </div>
                </div>
                <div style="height: 200px"></div>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/x-layui.js}" charset="utf-8"></script>
<script th:src="@{/js/jquery.min.js}" charset="utf-8"></script>
<script>

    if ($("input[name='id']").val() != "") {    //根据id查询数据
        var id = $("input[name='id']").val()
        $.get("/community/" + id, function (data) {
            console.log(data)
            $("input[name='communityNo']").val(data.data.communityNo)
            $("input[name='communityName']").val(data.data.communityName)
            $("input[name='address']").val(data.data.address)
            $("input[name='area']").val(data.data.area)
            $("input[name='dong']").val(data.data.dong)
            $("input[name='familyNum']").val(data.data.familyNum)
            $("input[name='afforest']").val(data.data.afforest)
            if (data.data.filePath != "" && data.data.filePath != null) {
                $("#pimages").attr('src', data.data.filePath);
                $("#avatar").val(data.data.filePath);
                $(".imgs").show();
            }
            $("input[name='developers']").val(data.data.developers)
            $("input[name='property']").val(data.data.property)
            if (data.data.state == 1) {
                $('input:radio:first').attr('checked', 'true');
            }
            if (data.data.state == 2) {
                $('input:radio:last').attr('checked', 'true');
            }
        })
    }


    layui.use(['element', 'layer', 'form'], function () {
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        form = layui.form();
        okLoading.close($);
        form.render();
        //以上模块根据需要引入


        //监听提交
        form.on('submit(add)', function (data) {
            var communityNo = $("input[name='communityNo']").val();
            var community_name = $("input[name='communityName']").val();
            var address = $("input[name='address']").val();
            var area = $("input[name='area']").val();
            var dong = $("input[name='dong']").val();
            var familyNum = $("input[name='familyNum']").val();
            var afforest = $("input[name='afforest']").val();
            var developers = $("input[name='developers']").val();
            var property = $("input[name='property']").val();
            if (communityNo == "") {
                layer.msg('小区编号不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (community_name == "") {
                layer.msg('小区名称不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (address == "") {
                layer.msg('坐落地址不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (area == "") {
                layer.msg('占地面积不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (address == "") {
                layer.msg('坐落地址不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (dong == "") {
                layer.msg('总栋数不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (familyNum == "") {
                layer.msg('总户数不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (afforest == "") {
                layer.msg('绿化率不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (developers == "") {
                layer.msg('小区开发商不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (property == "") {
                layer.msg('物业公司不能为空', {icon: 5, time: 2000});
                return false;
            }
            var data = data.field;
            console.log(data)
            if ($("input[name='id']").val() != "") {    //修改
                $.ajax({
                    type: "put",
                    url: "/community/update",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        console.log(data)
                        if (data.data == true) {
                            layer.msg(data.msg, {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        }
                        else {
                            layer.msg(data.info, {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }

            if ($("input[name='id']").val() == "") {    //增加
                $.ajax({
                    type: "post",
                    url: "/community/add",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        console.log(data)
                        if (data.data == true) {
                            layer.msg(data.msg, {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        }
                        else {
                            layer.msg(data.msg, {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }
        });
    })
</script>
<!--栏目缩略图上传-->
<script>
    function upload(obj) {
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        console.log(formData)
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type: "post",
            async: true,
            processData: false,
            contentType: false,
            url: "/upload/uploadFile",
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.msg == '执行成功') {
                    console.log(data.data);
                    layer.closeAll('loading');
                    $("#pimages").attr('src', data.data);
                    $("#avatar").val(data.data);
                    $(".imgs").show();
                    return false;
                } else {
                    layer.msg("操作失败", {icon: 2, time: 1000});
                }
            }
        });
    }
</script>

</body>
</html>